package com.tjec.project.projectLeaderWorkbeanch.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import com.tjec.common.utils.DateUtils;
import com.tjec.common.utils.SecurityUtils;
import com.tjec.common.utils.StringUtils;
import com.tjec.framework.web.domain.TreeSelect;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tjec.project.projectLeaderWorkbeanch.mapper.BmsMdExpertTypeMapper;
import com.tjec.project.projectLeaderWorkbeanch.domain.BmsMdExpertType;
import com.tjec.project.projectLeaderWorkbeanch.service.IBmsMdExpertTypeService;

/**
 * 专家专业分类Service业务层处理
 * 
 * @author tjec
 * @date 2020-04-21
 */
@Service
public class BmsMdExpertTypeServiceImpl implements IBmsMdExpertTypeService {
	@Autowired
	private BmsMdExpertTypeMapper bmsMdExpertTypeMapper;

	/**
	 * 查询专家专业分类
	 * 
	 * @param guid 专家专业分类ID
	 * @return 专家专业分类
	 */
	@Override
	public BmsMdExpertType selectBmsMdExpertTypeByGuid(String guid) {
		return bmsMdExpertTypeMapper.selectBmsMdExpertTypeByGuid(guid);
	}

	/**
	 * 查询专家专业分类
	 *
	 * @param guids 专家专业分类ID
	 * @return 专家专业分类
	 */
	@Override
	public List<BmsMdExpertType> selectBmsMdExpertTypeByGuids(String[] guids) {
		return bmsMdExpertTypeMapper.selectBmsMdExpertTypeByGuids(guids);
	}

	/**
	 * 查询专家专业分类列表
	 * 
	 * @param bmsMdExpertType 专家专业分类
	 * @return 专家专业分类
	 */
	@Override
	public List<BmsMdExpertType> selectBmsMdExpertTypeList(BmsMdExpertType bmsMdExpertType) {
		return bmsMdExpertTypeMapper.selectBmsMdExpertTypeList(bmsMdExpertType);
	}

	/**
	 * 新增专家专业分类
	 * 
	 * @param bmsMdExpertType 专家专业分类
	 * @return 结果
	 */
	@Override
	public int insertBmsMdExpertType(BmsMdExpertType bmsMdExpertType) {
		bmsMdExpertType.setCreateTime(DateUtils.getNowDate());
		bmsMdExpertType.setCreateBy(SecurityUtils.getLoginUser().getUser().getUserId());
		if (null == bmsMdExpertType.getGuid() || ("").equals(bmsMdExpertType.getGuid())) {
			bmsMdExpertType.setGuid(UUID.randomUUID().toString());
		}
		return bmsMdExpertTypeMapper.insertBmsMdExpertType(bmsMdExpertType);
	}

	/**
	 * 修改专家专业分类
	 * 
	 * @param bmsMdExpertType 专家专业分类
	 * @return 结果
	 */
	@Override
	public int updateBmsMdExpertType(BmsMdExpertType bmsMdExpertType) {
		bmsMdExpertType.setUpdateTime(DateUtils.getNowDate());
		bmsMdExpertType.setUpdateBy(SecurityUtils.getLoginUser().getUser().getUserId());
		return bmsMdExpertTypeMapper.updateBmsMdExpertType(bmsMdExpertType);
	}

	/**
	 * 批量删除专家专业分类
	 * 
	 * @param ids 需要删除的专家专业分类ID
	 * @return 结果
	 */
	@Override
	public int deleteBmsMdExpertTypeByIds(String[] ids) {
		return bmsMdExpertTypeMapper.deleteBmsMdExpertTypeByIds(ids);
	}

	/**
	 * 删除专家专业分类信息
	 * 
	 * @param id 专家专业分类ID
	 * @return 结果
	 */
	@Override
	public int deleteBmsMdExpertTypeById(String id) {
		return bmsMdExpertTypeMapper.deleteBmsMdExpertTypeById(id);
	}

	@Override
	public List<TreeSelect> buildExpertTypeTreeSelect(List<BmsMdExpertType> bmsMdExpertTypes) {
		List<BmsMdExpertType> expertTypeTrees = buildExpertTypeTree(bmsMdExpertTypes);
		return expertTypeTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
	}
	
	@Override
	public List<BmsMdExpertType> buildExpertTypeTree(List<BmsMdExpertType> bmsMdExpertTypes) {
		List<BmsMdExpertType> returnList = new ArrayList<BmsMdExpertType>();
		List<String> tempList = new ArrayList<String>();
		for (BmsMdExpertType bmsMdExpertType : bmsMdExpertTypes) {
			tempList.add(bmsMdExpertType.getGuid());
		}
		for (Iterator<BmsMdExpertType> iterator = bmsMdExpertTypes.iterator(); iterator.hasNext();) {
			BmsMdExpertType bmsMdExpertType = (BmsMdExpertType) iterator.next();
			// 如果是顶级节点, 遍历该父节点的所有子节点
			if (!tempList.contains(bmsMdExpertType.getParentId())) {
				recursionFn(bmsMdExpertTypes, bmsMdExpertType);
				returnList.add(bmsMdExpertType);
			}
		}
		if (returnList.isEmpty()) {
			returnList = bmsMdExpertTypes;
		}
		return returnList;
	}

	/**
	 * 获取子类专家分类通过guid
	 * @param guid 专家专业分类ID
	 * @return
	 */
	@Override
	public List<String> selectBmsMdExpertTypeChildrenByGuid(String guid) {
		List<String> bmsMdExpertTypes = bmsMdExpertTypeMapper.selectBmsMdExpertTypeChildrenByGuid(guid);
		return bmsMdExpertTypes;
	}

	/**
	 * 递归列表
	 */
	private void recursionFn(List<BmsMdExpertType> list, BmsMdExpertType t) {
		// 得到子节点列表
		List<BmsMdExpertType> childList = getChildList(list, t);
		t.setChildren(childList);
		for (BmsMdExpertType tChild : childList) {
			if (hasChild(list, tChild)) {
				// 判断是否有子节点
				Iterator<BmsMdExpertType> it = childList.iterator();
				while (it.hasNext()) {
					BmsMdExpertType n = (BmsMdExpertType) it.next();
					recursionFn(list, n);
				}
			}
		}
	}

	/**
	 * 得到子节点列表
	 */
	private List<BmsMdExpertType> getChildList(List<BmsMdExpertType> list, BmsMdExpertType t) {
		List<BmsMdExpertType> tlist = new ArrayList<BmsMdExpertType>();
		Iterator<BmsMdExpertType> it = list.iterator();
		while (it.hasNext()) {
			BmsMdExpertType n = (BmsMdExpertType) it.next();
			if (StringUtils.isNotNull(n.getParentId()) && t.getGuid().equals(n.getParentId())) {
				tlist.add(n);
			}
		}
		return tlist;
	}

	/**
	 * 判断是否有子节点
	 */
	private boolean hasChild(List<BmsMdExpertType> list, BmsMdExpertType t) {
		return getChildList(list, t).size() > 0 ? true : false;
	}

}
